//功能权限管理
var id;
var groupid;
var iconid;
var subid;
var menuid;
$(function() {

  var roleName = ly.getUrlParam("roleName");
  id = ly.getUrlParam("id");
  groupid = ly.getUrlParam("groupid");
  iconid = ly.getUrlParam("iconid");
  subid = ly.getUrlParam("subid");
  menuid = ly.getUrlParam("menuid");
  console.log(roleName);
  $("#roleName").html(roleName);
  $("#id").val(id);
  //权限分配回显
  var url = site.apiUrl + "/back/role/selectRoleThisMenuAndFuncByRoleId";
  requestPost(url, { "id": id }, funcAndMenuSuccess, 'post', false, false);

  $("body").on("click", "#save-btn-Function", function() {
    save();
  });

});

function funcAndMenuSuccess(data) {
  if (data.code = 200) {
    $("#funList").permissionList({
      "echodataMenu": data.data.menu,
      // "echodataFunc": data.data.func,
      "orgId": ""
    });
  } else {
    ly.toast(data.data);
  }
}

function save() {
  var data = $('#funList').permissionList.returndata();
  // var fun = data.fun;
  var menu = data.menu;

  //console.log(menu.length);
  if (menu.length > 0) {
    var m = "";
    var f = "";
    for (i = 0; i < menu.length; i++) {
      m += menu[i] + ",";
    }
    // for (i = 0; i < fun.length; i++) {
    //   f += fun[i] + ",";
    // }
    var obj = {
      'id': id,
      // 'funId': f,
      'menuId': m
    };
    var url = site.apiUrl + "/back/role/updateRoleThisMenuAndFunc";
    requestPost(url, obj, addCallback, 'post', false, false);

  } else {
    ly.toast("勾选不可为空");
  }

}

function addCallback(data) {
  console.log(data);
  if (data.code = 200) {
    ly.alert("分配成功", function() {
      ly.goto("role.html");
    });
  } else {
    ly.toast(data.data);
  }
}